package ui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JFileChooser;

import main.ErrorException;
import main.ErrorHandler;
import define.Common;
import define.Config;
import excelBase.ExcelData;

public class OpenHandle extends MouseAdapter
{
	public static boolean m_isOpen = false;
	private boolean m_isAdd;
	
	public OpenHandle(boolean isAdd)
	{
		super();
		m_isAdd = isAdd;
	}
	
	public void mouseClicked(MouseEvent e)
	{
		if(Common.m_isRunning) 
		{
			return;
		}
		JFileChooser chooser = new JFileChooser(Common.m_path);
		chooser.setMultiSelectionEnabled(true);
		chooser.setSize(Config.WINDOW_W / 2, Config.WINDOW_H / 2);
		int choice = chooser.showOpenDialog(Window.m_instance.getMainWindow());
		if(choice == 0) 
		{
			ErrorHandler.reset();
			File[] files = chooser.getSelectedFiles();
			Common.m_path = Common.getPath(files[0].getAbsolutePath());
			String scriptName = Window.m_instance.getScriptName();
			boolean isSetScriptName = scriptName.compareTo("") != 0;
			if(!m_isAdd)
			{
				ExcelData.clearData();
				Window.m_instance.clearText();
			}
			CompileHandle.m_isCompile = false;
			m_isOpen = true;
			for(File temp: files)
			{
				try 
				{
					if(isSetScriptName)
					{
						Common.m_scriptSheet = scriptName;
					}
					ExcelData.readExcel(temp);
					Window.m_instance.addText("load sucess: " + temp.getName());
				} 
				catch(ErrorException e2) 
				{
					m_isOpen = false;
					new ErrorHandler(e2);
				}
			}
		}
	}
}
